package cn.tedu.shoot;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;//边框
import javax.swing.JPanel;//面板
import java.awt.Graphics; //绘画

import java.util.Timer;     //
import java.util.TimerTask; //
import java.util.Random;
import java.util.Arrays;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.awt.Color;//画笔的颜色
import java.awt.Font;//画笔的规格
/**
 * 开始方法gits试试
 * @author yy
 *
 */
public class ShootGame extends JPanel{
	public static final int WIDTH =400;
	public static final int HEIGHT =654;
	
	public static BufferedImage background;
	public static BufferedImage start;
	public static BufferedImage pause;
	public static BufferedImage gameover;
	public static BufferedImage airplane;
	public static BufferedImage bee;
	public static BufferedImage bullet;
	public static BufferedImage hero0;
	public static BufferedImage hero1;
	
	static{
		try{
			background =ImageIO.read(ShootGame.class.getResource("background.png"));
			start =ImageIO.read(ShootGame.class.getResource("start.png"));
			pause =ImageIO.read(ShootGame.class.getResource("pause.png"));
			gameover =ImageIO.read(ShootGame.class.getResource("gameover.png"));
			airplane =ImageIO.read(ShootGame.class.getResource("airplane.png"));
			bee =ImageIO.read(ShootGame.class.getResource("bee.png"));
			bullet =ImageIO.read(ShootGame.class.getResource("bullet.png"));
			hero0 =ImageIO.read(ShootGame.class.getResource("hero0.png"));
			hero1 =ImageIO.read(ShootGame.class.getResource("hero1.png"));
		
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static final int START =0;
	public static final int RUNNING =1;
	public static final int PAUSE =2;
	public static final int GAME_OVER =3;
	private int state = START;
	
	private Hero hero =new Hero();//1个英雄机对象
	private FlyingObject[] flyings ={};//1堆敌人
	private Bullet[] bullets = {};//一堆子弹
	
	//重写方法  画笔
	public void paint(Graphics g){
	    g.drawImage(background,0,0,null);
	    paintHero(g);
	    paintFlyingObject(g);
	    paintBullets(g);
	    paintScoreAndLife(g);
	    paintState(g);
	}
	public void paintHero(Graphics g){
		g.drawImage(hero.image,hero.x,hero.y,null);
	}
	public void paintFlyingObject(Graphics g){
		for(int i=0;i<flyings.length;i++){
			FlyingObject f =flyings[i];
			g.drawImage(f.image,f.x,f.y,null);
		}
	}
	public void paintBullets(Graphics g){
		for(int i=0;i<bullets.length;i++){
			Bullet b =bullets[i];
			g.drawImage(b.image,b.x,b.y,null);
		}
	}
	public void paintScoreAndLife(Graphics g){
		g.setColor(new Color(0xFF0000));
		g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,24 ));
		g.drawString("SCORE: "+score,10,25);
		g.drawString("LIFE: "+hero.getLife(),10,45);
	}
	
	public void paintState(Graphics g){
		switch(state){
		case START:
			g.drawImage(start,0,0,null);
			break;
		case PAUSE:
			g.drawImage(pause,0,0,null);
			break;
		case GAME_OVER:
			g.drawImage(gameover,0,0,null);
			break;
		}
	}
	
/** 随机生成敌人对象*/	
	public FlyingObject nextOne(){//随机生成对象
		Random ran =new Random();
		int type =ran.nextInt(30);
		if(type<2){//控制生成不同敌人类型的概率
			return new Bee();
		}else{
			return new Airplane();
		}
	}
	

	int flyIndex =0;
	public void enterAction(){
		flyIndex++;
		if(flyIndex%40==0){
			FlyingObject obj =nextOne();
			flyings =Arrays.copyOf(flyings, flyings.length+1);
			flyings[flyings.length-1] =obj;
		}
	}
	

	public void stepAction(){
		hero.step();
		for(int i=0;i<flyings.length;i++){
			flyings[i].step();
		}
		for(int i=0;i<bullets.length;i++){
			bullets[i].step();
		}
	}

	int shootIndex =0;
	public void shootAction(){
		shootIndex++;
		if(shootIndex%20==0){
			Bullet[] bs=hero.shoot();
			bullets =Arrays.copyOf(bullets, bullets.length+bs.length);
			System.arraycopy(bs, 0, bullets, bullets.length-bs.length, bs.length);
		}
	}
	

	public void outOfBoundsAction(){
		int index =0;
		FlyingObject[] flyinglives =new FlyingObject[flyings.length];
		for(int i=0;i<flyings
				.length;i++){
			FlyingObject f =flyings[i];
			if(!f.outOfBounds()){
				flyinglives[index] =f;
				index++;
			}
		}
		flyings =Arrays.copyOf(flyinglives,index);
		
		index =0;
		Bullet[] bulletlives =new Bullet[bullets.length];
		for(int i=0;i<bullets.length;i++){
			Bullet b =bullets[i];
			if(!b.outOfBounds()){
				bulletlives[index] =b;
				index++;
			}
		}
		bullets =Arrays.copyOf(bulletlives, index);
		
	}

	int score =0;
	public void bangAction(){
		for(int i=0;i<bullets.length;i++){
			Bullet b =bullets[i];
			for(int j=0;j<flyings.length;j++){
				FlyingObject f =flyings[j];
				if(f.shootBy(b)){
					if(f instanceof Enemy){
						Enemy e =(Enemy)f;
						score+= e.getScore();
					}
					if(f instanceof Award){
						Award a =(Award)f;
						int type =a.getType();
						switch(type){
						case Award.DOUBLE_FIRE:
							hero.addDoubleFire();
							break;
						case Award.LIFE:
							hero.addLife();
							break;
						}
					}
			    
					FlyingObject t =flyings[j];
					flyings[j] =flyings[flyings.length-1];
					flyings[flyings.length-1] =t;
					flyings =Arrays.copyOf(flyings, flyings.length-1);
					
					Bullet bu =bullets[i];
					bullets[i] =bullets[bullets.length-1];
					bullets[bullets.length-1] =bu;
					bullets =Arrays.copyOf(bullets,bullets.length-1);
				}
			}
		}
	}

	public void hitAction(){
		for(int i=0;i<flyings.length;i++){
			FlyingObject f =flyings[i];
			if(hero.hit(f)){
				hero.subtractLife();
				hero.clearDoubleFire();//
				
				FlyingObject t =flyings[i];
				flyings[i] =flyings[flyings.length-1];
				flyings[flyings.length-1] =t;
				flyings =Arrays.copyOf(flyings, flyings.length-1);
				/*
				 *   
				flyings[i] =flyings[flyings.length-1];
				flyings =Arrays.copyOf(flyings, flyings.length-1);
				*/
			}
		}
	}

	public void checkGameOverAction(){
		if(hero.getLife()<=0){
			state =GAME_OVER;
		}
	}
	

	public void action(){
		
		MouseAdapter l =new MouseAdapter(){
		
			public void mouseMoved(MouseEvent e){
				if(state==RUNNING){
					int x =e.getX();
					int y =e.getY();
					hero.moveTo(x, y);
				}
			}
		
			public void mouseClicked(MouseEvent e){
				switch(state){
				case START:
					state =RUNNING;
					break;
				case GAME_OVER:
					score =0;
					hero =new Hero();
					flyings =new FlyingObject[0];
					bullets =new Bullet[0];
					state =START;
					break;
				}
			}
		
			public void mouseExited(MouseEvent e){
				if(state==RUNNING){
					state =PAUSE;
				}
			}
		
			public void mouseEntered(MouseEvent e){
				if(state==PAUSE){
					state =RUNNING;
				}
			}
		};
		this.addMouseListener(l);
		this.addMouseMotionListener(l);
		
		Timer timer =new Timer();
		int in =10;
		timer.schedule(new TimerTask(){
			public void run(){
				if(state==RUNNING){
					enterAction();
					stepAction(); 
					shootAction();
					outOfBoundsAction();
					bangAction();
					hitAction();
					checkGameOverAction();
				}
				repaint();
			}
		},in,in);
	}
	
	public static void main(String[] args) {
		JFrame frame =new JFrame("FLY");
		ShootGame game =new ShootGame();
		frame.add(game);
		frame.setSize(WIDTH, HEIGHT);
		frame.setAlwaysOnTop(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		game.action();

	}

}
